package com.bizark.rpc.common.tool;

import com.google.common.collect.Maps;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.Swap;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;


public class SystemInfoUtil {


    private static Sigar sigar = new Sigar();

    private SystemInfoUtil() {
    }

    public static Sigar getSigar() {
        return sigar;
    }

    /**
     * 获取主机相关信息
     *
     * @return
     * @throws UnknownHostException
     */
    public static Map<String, Object> getHostInfo() {
        Map<String, Object> hostMap = Maps.newHashMap();
        Runtime runtime = Runtime.getRuntime();
        Properties props = System.getProperties();
        InetAddress addr;
        String ip = null;
        String hostName = null;
        try {
            addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress();// 本地ip地址
            hostName = addr.getHostName();// 本地主机名
        } catch (UnknownHostException e) {
            e.printStackTrace();
            ip = "无法获取主机IP";
            hostName = "无法获取主机名";
        }
        Map<String, String> map = System.getenv();

        String userName = map.get("USERNAME");// 获取用户名
        String computerName = map.get("COMPUTERNAME");// 获取计算机名
        String userDomain = map.get("USERDOMAIN");// 获取计算机域名
        String osName = props.getProperty("os.name");// 操作系统的名称
        String osArch = props.getProperty("os.arch");// 操作系统的构架
        String osVersion = props.getProperty("os.version");// 操作系统的版本
        String availableProcessors = String.valueOf(runtime.availableProcessors());// JVM可以使用的处理器个数
        String javaVersion = props.getProperty("java.version");// Java的运行环境版本
        String javaVendor = props.getProperty("java.vendor");// Java的运行环境供应商
        String javaVendorUrl = props.getProperty("java.vendor.url");// Java供应商的URL
        String javaHome = props.getProperty("java.home");// Java的安装路径
        String ioTmpdir = props.getProperty("java.io.tmpdir");// 默认的临时文件路径
        String userDir = props.getProperty("mapper.user.dir");// 用户的当前工作目录

        hostMap.put("ip", ip);
        hostMap.put("userName", userName);
        hostMap.put("computerName", computerName);
        hostMap.put("userDomain", userDomain);
        hostMap.put("hostName", hostName);
        hostMap.put("osName", osName);
        hostMap.put("osArch", osArch);
        hostMap.put("osVersion", osVersion);
        hostMap.put("availableProcessors", availableProcessors);
        hostMap.put("javaVersion", javaVersion);
        hostMap.put("javaVendor", javaVendor);
        hostMap.put("javaVendorUrl", javaVendorUrl);
        hostMap.put("javaHome", javaHome);
        hostMap.put("ioTmpdir", ioTmpdir);
        hostMap.put("userDir", userDir);

        return hostMap;
    }


    /**
     * 获取内存相关信息
     *
     * @return
     */
    public static Map<String, Object> getMonoryInfo() {
        Map<String, Object> momoryInfoMap = Maps.newHashMap();

        Runtime runtime = Runtime.getRuntime();
        Sigar sigar = getSigar();
        try {
            Mem mem = sigar.getMem();
            long totalMomory = mem.getTotal() / 1024L / 1024L;//内存总量
            long usedMomory = mem.getUsed() / 1024L / 1024L;//当前内存使用量
            long freeMomory = mem.getFree() / 1024L / 1024L;//当前内存剩余量
            double momoryRate = NumberUtil.div(usedMomory, totalMomory, 2) * 100;

            Swap swap = sigar.getSwap();
            long totalSwap = swap.getTotal() / 1024L / 1024L;//交换区总量
            long usedSwap = swap.getUsed() / 1024L / 1024L;//当前交换区使用量
            long freeSwap = swap.getFree() / 1024L / 1024L;//当前交换区剩余量
            double swapRate = NumberUtil.div(usedSwap, totalSwap, 2) * 100;

            long jvmTotalMomory = runtime.totalMemory();//java总内存
            long jvmFreeMomory = runtime.freeMemory();//JVM剩余内存
            long jvmUseMomory = jvmTotalMomory - jvmFreeMomory;//JVM使用内存
            double jvmRate = NumberUtil.div(jvmUseMomory, jvmTotalMomory, 2) * 100;

            momoryInfoMap.put("momoryRate", momoryRate);
            momoryInfoMap.put("swapRate", swapRate);
            momoryInfoMap.put("jvmRate", jvmRate);

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(momoryInfoMap);
        return momoryInfoMap;
    }


    /**
     * 获取cpu相关信息
     *
     * @return
     */
    public static Map<String, Object> getCpuInfo() {
        Map<String, Object> cpuInfoMap = Maps.newHashMap();

        Sigar sigar = getSigar();
        CpuPerc cpuList[];
        try {
            cpuList = sigar.getCpuPercList();
            double cpuRate = 0.0;
            for (CpuPerc cpu : cpuList) {
                cpuRate += cpu.getCombined();
            }
            cpuRate = NumberUtil.div(cpuRate, cpuList.length, 2) * 100;
            cpuInfoMap.put("cpuRate", cpuRate);

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(cpuInfoMap);
        return cpuInfoMap;
    }


}
